﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eb.StockManagement;
using Eb.MessageBox;
using ProductManagement;
public partial class Modules_StockManagement_DepartmentMove : System.Web.UI.UserControl
{
    StockManagement stockMgmt = new StockManagement();
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateProductGrid();
        }
    }

    protected void PopulateProductGrid()
    {
       DataSet productDS= stockMgmt.GetProductDtls();
       grvProductDetails.DataSource = productDS;
       grvProductDetails.DataBind();
    }

    protected void grvProductDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int itemId = 0;
            int quantity = 0;
            GridViewRow row = (GridViewRow)grvProductDetails.Rows[e.RowIndex];
            Label lbl = (Label)row.FindControl("lblProductID");
            string strId = lbl.Text.Trim();
            if (strId != "" || strId != null)
                itemId = Convert.ToInt16(strId);
            TextBox txtQuantity = (TextBox)row.FindControl("txtQty");
            string qty = txtQuantity.Text;
            if (qty != "" || qty != null)
            {
                quantity = Convert.ToInt16(qty);
            }
            grvProductDetails.EditIndex = -1;
            // bool status = stockManagement.UpdateItemQty(1, quantity, itemId);

            int userId = 0;
            if (Session["UserId"] != null)
                userId = Convert.ToInt16(Session["UserId"].ToString());

            int productId = itemId;
            bool batchHistoryStatus = stockMgmt.InsertBatchHistory(productId.ToString(), quantity, 1, "", "", userId.ToString(), "", "", "");
            bool productStockStatus = stockMgmt.UpdateCurrentAvailabilityOfStock(productId, quantity, "");
            bool prodStatus = stockMgmt.UpdateProductList(quantity, productId);
            PopulateProductGrid();

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Failed to add Quantity!";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    protected void grvProductDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvProductDetails.EditIndex = -1;
        PopulateProductGrid();
    }
    protected void grvProductDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvProductDetails.EditIndex = e.NewEditIndex;
        PopulateProductGrid();
    }
}
